package com.hundredapp.oa.repository;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.hundredapp.oa.dao.GenericDao;
import com.hundredapp.oa.domain.OaClass;
import com.hundredapp.oa.domain.OaElect;
import com.hundredapp.oa.domain.OaSign;
import com.hundredapp.oa.domain.OaSlow;
import com.hundredapp.oa.domain.OaStudent;
import com.hundredapp.oa.domain.OaSubject;

@Repository
public class StudentMangerImpl implements StudentManager{

	@Autowired
	private GenericDao<OaStudent, Integer> studentDao;
	@Autowired
	private GenericDao<OaClass, Integer> classDao;
	@Autowired
	private GenericDao<OaElect, Integer> electDao;
	@Autowired
	private GenericDao<OaSign, Integer> signDao;
	@Autowired
	private GenericDao<OaSlow, Integer> slowDao;
	
	@Override
	public Integer save(OaStudent student) {
		return studentDao.save(student);
	}
	
	@Override
	public void update(OaStudent student) {
		studentDao.modify(student);
	}

	public void setStudentDao(GenericDao<OaStudent, Integer> studentDao) {
		this.studentDao = studentDao;
	}

	@Override
	public List<OaStudent> findAll(String condition) {
		return studentDao.find(condition);
	}

	@Override
	public OaStudent findById(Integer studentid) {
		return studentDao.findById(studentid);
	}

	@Override
	public List<OaStudent> find(String condition, int start, int limit) {
		return studentDao.find(condition, start, limit);
	}

	@Override
	public int count(String condition) {
		return studentDao.count(condition);
	}

	@Override
	public int findlesson(int classid, int studentid) {
		int leslesson = 0;
		if(classid>0){
			OaClass class1 = classDao.findById(classid);
			Integer hadlesson = signDao.count(" studentId = "+studentid+" and classId="+class1.getId()+" and state = 1");
			OaSubject sub = class1.getOaSubject();
			List<OaElect> elects = electDao.find(" oaSubject.id = "+sub.getId());
			double price = 0;
			for(OaElect elect:elects){
				price+=elect.getPrice();
			}
			int lesson = (int) (price/(sub.getPrice()/sub.getLesson()));
			int chargeback = 0;//缓修占位费，每+1代表一次占位
			for(OaSlow slow:slowDao.find(" oastudents.id = "+studentid+" and oasubjects.id = "+sub.getId())){
				if(slow.getChargeback()>0)
					chargeback++;
			}
			leslesson = lesson-hadlesson-chargeback*2;
		}
		return leslesson;
	}	
	
}
